package com.googlecode.projects.tests;

import java.sql.*;

public class JDBCViewTables {
	public static void main(String[] args) {

		/**
		 * Command line display.
		 */
		System.out.println("Tables in the current database are:");

		/**
		 * Create a new connection and set it to null.
		 */
		Connection connection = null;

		/**
		 * Create a new string to hold the name of the database.
		 */
		String database = "jdbc:derby:/DerbyDB/AssetDB";

		/**
		 * Create a new string to hold the name of the database driver.
		 */
		String databaseDriver = "org.apache.derby.jdbc.EmbeddedDriver";

		/**
		 * Try to display all the tables located in the database.
		 */
		try {
			Class.forName(databaseDriver);
			connection = DriverManager.getConnection(database);
			try {
				DatabaseMetaData metaData = connection.getMetaData();
				String[] types = { "TABLE" };
				ResultSet resultSet = metaData
						.getTables(null, null, "%", types);
				while (resultSet.next()) {
					String tableName = resultSet.getString("TABLE_NAME");
					System.out.println(tableName);
				}
				connection.close();
			} catch (SQLException s) {
				System.out
						.println("The database does not contain any tables!!!");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
